package com.car.carmgr.service.impl;

import com.car.carmgr.bean.Role;
import com.car.carmgr.bean.User;
import com.car.carmgr.mapper.UserMapper;
import com.car.carmgr.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(User user) {
        return userMapper.login(user);
    }

    @Override
    public int insertUser(User user) {
        return userMapper.insertUser(user);
    }

    @Override
    public int deleteUserById(Integer userId) {
        return userMapper.deleteUserById(userId);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    @Override
    public PageInfo<User> findUserByWhere(String loginName,String realName,String identity,String address,String phone,Integer sex, Integer page,Integer limit) {
        PageHelper.startPage(page,limit);
        List<User> list = userMapper.findUserByWhere(loginName,realName,identity,address,phone,sex);
        PageInfo<User> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public Integer getCountByWhere(String loginName,String realName,String identity,String address,String phone,Integer sex) {
        return userMapper.getCountByWhere(loginName,realName,identity,address,phone,sex);
    }

    @Override
    public int resetUserPwd(Integer userId) {
        return userMapper.resetUserPwd(userId);
    }

    @Override
    public int deleteSomeUser(String [] split) {
        int row = 0;
        if(split != null && split.length != 0){

            for(String userId : split){
                row += userMapper.deleteUserById(Integer.parseInt(userId));
            }
        }
        return row;

    }

    @Override
    public int updateRoleUser(Integer userId, Integer[] rids) {
        int i = userMapper.delRoleUserByUserId(userId);
        int i1 = userMapper.insRoleUserByUserId(userId, rids);
        return i+i1;
    }
}
